package com.fzu.ess.ess.service.Impl;

import com.fzu.ess.common.Const;
import com.fzu.ess.common.service.ImageService;
import com.fzu.ess.ess.dao.ProfileDao;
import com.fzu.ess.ess.entity.Profile;
import com.fzu.ess.ess.service.ProfileService;
import com.fzu.ess.sys.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by ercha on 2015/10/25.
 */
@Service
public class ProfileServiceImpl implements ProfileService {
    @Autowired
    private ProfileDao profileDao;

    @Autowired
    private ImageService imageService;

    @Override
    public String create(Profile profile) {
        try {
            return (String) profileDao.save(profile);
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Profile get() {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User user = (User)session.getAttribute(Const.SESSION.USER);

        if(user == null){
            return null;
        }
        try {
            return profileDao.get("from Profile p where p.user.id = '" + user.getId() + "' and p.delFlag = " + Const.DEL_FLAG_NORMAL);
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Profile getByUserId(String userId) {
        if(userId == null){
            return null;
        }
        try {
            return profileDao.get("from Profile p where p.user.id = '" + userId + "' and p.delFlag = " + Const.DEL_FLAG_NORMAL);
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Profile> getAll() {
        try{
            return profileDao.find("from Profile");
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean update(Profile profile) {
        try {
            String imgName;

            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            User user = (User)session.getAttribute(Const.SESSION.USER);
            profile.setUser(user);

            Profile p = profileDao.get("from Profile p where p.user.id = '" + user.getId() + "'");
            profile.setId(p.getId());
            profile.setUpdateDate(new Date());

            //前台没有传过来，则用数据库已存在的
//            System.out.println("p.getAvatar(): " + p.getAvatar());
//            System.out.println("Profile.getAvatar(): " + profile.getAvatar());
            if(profile.getAvatar() == null || profile.getAvatar().length()<1){
                profile.setAvatar(p.getAvatar());
                imgName = null;
            } else{
                imgName = p.getAvatar();
            }
            profileDao.merge(profile);
            session.setAttribute("avatar", "/images/@s" + profile.getAvatar());
            return imgName == null || imgName.length()<5 || imageService.deleteFromDisk(imgName);
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
